package com.meatball.iot.driver.smokealarm

import com.alibaba.fastjson.JSON
import com.meatball.core.SpringUtils
import com.meatball.iot.common.mqtt.MqttStrategy
import com.meatball.iot.driver.smokealarm.data.DiInfoDto
import com.meatball.iot.driver.smokealarm.data.document.SmokeAlarmDocument
import com.meatball.iot.driver.smokealarm.enums.DeviceStatusEnum
import com.meatball.iot.driver.smokealarm.repository.SmokeAlarmRepository
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

/**
 * TODO 烟感数据处理服务类
 *
 * @author 张翔宇
 * @since 2024年6月7日
 */
@Service
class SmokeAlarmService(private val smokeAlarmRepository: SmokeAlarmRepository) : MqttStrategy {
    companion object {
        private val logger = LoggerFactory.getLogger(SmokeAlarmService::class.java)
//        private val smokeAlarmRepository = SpringUtils.getBean(SmokeAlarmRepository::class.java)
    }

    override fun toStart(message: String, topic: String) {
        logger.info("收到消息：$message")
        val sn = topic.split("/")[1]
        val data = JSON.parseObject(message, DiInfoDto::class.java)
        smokeAlarmRepository.save(
            SmokeAlarmDocument(
                deviceID = sn,
                deviceStatus = DeviceStatusEnum.fromCode(data.data.one)
            )
        )
    }
}